package com.gs.wzmanager.config.webmvc;


import com.gs.wzmanager.config.interceptor.TracerInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.filter.CharacterEncodingFilter;
import org.springframework.web.servlet.ViewResolver;
import org.springframework.web.servlet.config.annotation.*;
import org.springframework.web.servlet.view.InternalResourceViewResolver;

import javax.servlet.Filter;

/**
 * Created with IntelliJ IDEA.
 * User: Legolas
 * Date: 2017/8/3 0003
 */
//@EnableWebMvc 等同于 MVC:annotation 注解驱动
@EnableWebMvc
@Configuration
public class MvcConfig extends WebMvcConfigurerAdapter {

    @Override public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")
                .allowedOrigins("*")
                .allowedMethods("GET", "HEAD", "POST", "PUT", "PATCH", "DELETE", "OPTIONS", "TRACE");
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        //添加跨域访问
        //registry.addInterceptor(new CORSInterceptor());
        registry.addInterceptor(new TracerInterceptor());
    }

    /**
     * 定义静态资源访问路径
     * */
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        //registry.addResourceHandler("/assets/**").addResourceLocations("/assets/");
    }

    @Bean
    public Filter characterEncodingFilter() {
        CharacterEncodingFilter characterEncodingFilter = new CharacterEncodingFilter();
        characterEncodingFilter.setEncoding("UTF-8");
        characterEncodingFilter.setForceEncoding(true);
        return characterEncodingFilter;
    }

    /**same as <mvc:default-servlet-handler/>*/
    @Override
    public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
        configurer.enable();
    }

    @Bean
    public ViewResolver viewResolver(){
        InternalResourceViewResolver resolver = new InternalResourceViewResolver();
        resolver.setPrefix("/views/");
        resolver.setSuffix(".html");
        return resolver;
    }

  /*  @Override
    public void configureViewResolvers(ViewResolverRegistry registry) {
        //对返回JSON支持
        registry.enableContentNegotiation(new MappingJackson2JsonView());
    }
*/
}
